#!/usr/bin/env node
const chalk = require('chalk');//可以对console输出内容有颜色标识。

console.log('Hello, This is ndc—cli!');

console.log(chalk.green('init创建'))

const fs = require('fs')

const program = require('commander');//让命令行输入指令

const download = require('download-git-repo');//从 Github下载内容插件

const inquirer = require('inquirer');//可以通过命令行进行交互

const ora = require('ora');//显示进度条

const symbols = require('log-symbols');//加些状态图标

program
    .version(require('./package').version, '-v, --version')
    .command('add <name>') //输入nbc add <项目名>就可以执行模板安装
    .action((name) => {
        console.log(name)
        inquirer.prompt([
            {
                type: 'input',
                name: 'author',
                message: '请输入作者名'
            }
        ]).then((answers) => {
            console.log(answers.author)
            const lqProcess = ora('正在创建...')
            lqProcess.start()
            download('direct:https://github.com/vuejs/vue.git',
                name, { clone: true }, (err) => {
               
                    if (err) {
                        lqProcess.fail()
                        console.log(symbols.error, chalk.red(''))
                    } else {
                        lqProcess.succeed()
                        const fileName = `${name}/package.json`
                        const meta = {
                            name,
                            author: answers.author
                        }
                        if (fs.existsSync(fileName)) {
                            const content = fs.readFileSync(fileName).toString();
                            const result = handlebars.compile(content)(meta);
                            fs.writeFileSync(fileName, result);
                        }
                        console.log(symbols.success, chalk.green('创建成功！'))
                    }
                })
        })

    })
program.parse(process.argv)


